let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({
  data: {
    customKeyboard: null,
    customCarNumInput: null,
    customCarNumInputIndex: 0,
    switch: false,
    inpVal: '',
    show: true,
    plate_desc:[],
    numbers: [{
      value: '',
      focus: true
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false,
      eco: true
    }]
  },

  onLoad() {
    const that = this;
    that.getInit()
  },
  onSwitch() {
    const that = this;
    that.setData({
      switch: !that.data.switch,
      inpVal: '',
      show: false
    })
    if (!that.data.switch) {
      that.onReady()
      that.setData({
        show: true
      })
    }
  },
  onReady() {
    this.setData({
      customCarNumInputIndex: 0,
      customKeyboard: this.selectComponent('#customKeyboard'),
      customCarNumInput: this.selectComponent('#customCarNumInput')
    });

    this.changeKerboard(1);
  },
  getInit() {
    const that = this;
    myRequest.EasyHttp.post(api.reqUrls.getTips).then(res => {
      that.setData({
        plate_desc: res.data.result.plate_desc
      })
    })
  },
  changeKerboard(type) {
    this.data.customKeyboard.change(type);
    this.data.customKeyboard.show();
  },

  onInputFocus(e) {
    const index = e.detail;
    if (index == 0) {
      this.changeKerboard(1);
    } else if (index == 7) {
      this.changeKerboard(3);
    } else {
      this.changeKerboard(2);
    }
    this.setData({
      customCarNumInputIndex: index,
    });
  },

  onKeyboardClick(e) {
    this.data.customCarNumInput.change({
      index: this.data.customCarNumInputIndex,
      value: e.detail,
      remove: false
    })
  },

  onKeyboardDelete(e) {
    this.data.customCarNumInput.change({
      index: this.data.customCarNumInputIndex,
      value: '',
      remove: true
    })
  },
  onInput(e) {
    const that = this;
    that.setData({
      inpVal: e.detail.value
    })
  },
  showCarNo(e) {
    const that = this;
    const cardnumber = wx.getStorageSync('cardnumber');
    if (!that.data.switch) {
      let numbers = this.data.customCarNumInput.data.numbers.map(item => item.value).join('')
      if (numbers) {
        let data = {
          cardnumber: cardnumber,
          platenumber: numbers
        }
        wx.showLoading({
          title: '加载中...',
        })
        myRequest.EasyHttp.post(api.reqUrls.addLicensePlate, data).then(res => {
          wx.hideLoading();
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
          if (res.data.status == 1) {
            let pages = getCurrentPages(); //获取小程序页面栈
            let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
            beforePage.getData(); //触发上个页面自定义的shuaxin方法
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              confirmText: '我知道了',
              confirmColor: '#ff5151',
              success(e) {
                if (e.confirm) {

                }
              }
            })

          }

        })
      } else {
        wx.showToast({
          title: '请选择车牌号',
          icon: 'error',
          duration: 2000
        })
      }
    } else {
      if (that.data.inpVal) {
        let data = {
          cardnumber: cardnumber,
          platenumber: that.data.inpVal
        }
        wx.showLoading({
          title: '加载中...',
        })
        myRequest.EasyHttp.post(api.reqUrls.addLicensePlate, data).then(res => {
          wx.hideLoading();
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
          if (res.data.status == 1) {
            let pages = getCurrentPages(); //获取小程序页面栈
            let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
            beforePage.getData(); //触发上个页面自定义的shuaxin方法
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              confirmText: '我知道了',
              confirmColor: '#ff5151',
              success(e) {
                if (e.confirm) {

                }
              }
            })

          }

        })
      } else {
        wx.showToast({
          title: '请选择车牌号',
          icon: 'error',
          duration: 2000
        })
      }
    }

  }
})